﻿using System;

namespace Mozi.Live.SDP
{
    /// <summary>
    /// RTPMAP属性值
    /// </summary>
    public class SDPAttributeRTPMap
    {
        /// <summary>
        /// 荷载类型
        /// </summary>
        public byte PayloadType { get; set; }
        /// <summary>
        /// 编码格式
        /// </summary>
        public string EncodingName { get; set; }
        /// <summary>
        /// 采样频率HZ
        /// </summary>
        public int ClockRate { get; set; }
        /// <summary>
        /// audio声道，video无此参数
        /// </summary>
        public int Channels { get; set; }
        /// <summary>
        /// 转为rtpmap:{MediaType} {Encoding}/{ClockRate}/{Channels}
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (Channels > 0)
            {
                return $"{PayloadType} {EncodingName}/{ClockRate}/{Channels}";
            }
            else
            {
                return $"{PayloadType} {EncodingName}/{ClockRate}";
            }
        }
        /// <summary>
        /// 解析
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static SDPAttributeRTPMap Parse(string text)
        {
            SDPAttributeRTPMap map = new SDPAttributeRTPMap();
            string[] arr = text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            map.PayloadType = byte.Parse(arr[0]);
            string[] descs = arr[1].Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            map.EncodingName = descs[0];
            map.ClockRate = int.Parse(descs[1]);
            if (descs.Length > 2)
            {
                map.Channels = int.Parse(descs[2]);
            }
            return map;
        }
    }
}
